package io.renren.modules.sys.controller;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.sys.entity.SysBuildEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysBuildService;
import io.renren.modules.sys.service.SysUserService;

/**
 * 
 * 
 * @author pfp
 * @email
 * @date 2017-07-14 20:32:21
 */
@RestController
@RequestMapping("/sys/build")
public class SysBuildController extends AbstractController {

	@Autowired
	private SysBuildService sysBuildService;

	@Autowired
	private SysUserService sysUserService;

	/**
	 * 列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping("/list")
	@RequiresPermissions("sys:build:list")
	public R list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);

		List<SysBuildEntity> sysBuildList = sysBuildService.queryList(query);
		int total = sysBuildService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(sysBuildList, total, query.getLimit(), query.getPage());
		List<SysBuildEntity> list = (List<SysBuildEntity>) pageUtil.getList();
		for (SysBuildEntity sysBuildEntity : list) {
			SysUserEntity user = sysUserService.queryObject(sysBuildEntity.getCreateUser());
			if (user != null) {
				sysBuildEntity.setCreateUserName(user.getRealName());
			}
		}
		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{buildId}")
	@RequiresPermissions("sys:build:info")
	public R info(@PathVariable("buildId") Long buildId) {
		SysBuildEntity sysBuild = sysBuildService.queryObject(buildId);
		return R.ok().put("build", sysBuild);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:build:save")
	public R save(@RequestBody SysBuildEntity sysBuild) {
		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(sysBuild);
		Iterator<ConstraintViolation<Object>> iter = constraintViolations.iterator();
		while (iter.hasNext()) {
			String message = iter.next().getMessage();
			return R.error(message);
		}
		sysBuild.setCreateUser(getUserId());
		sysBuildService.save(sysBuild);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:build:update")
	public R update(@RequestBody SysBuildEntity sysBuild) {
		Set<ConstraintViolation<Object>> constraintViolations = validator.validate(sysBuild);
		Iterator<ConstraintViolation<Object>> iter = constraintViolations.iterator();
		while (iter.hasNext()) {
			String message = iter.next().getMessage();
			return R.error(message);
		}
		sysBuildService.update(sysBuild);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:build:delete")
	public R delete(@RequestBody Long[] buildIds) {
		sysBuildService.deleteBatch(buildIds);
		return R.ok();
	}

	/**
	 * 下拉框数据
	 */
	@RequestMapping("/dropDownList")
	public R dropDownList() {
		return sysBuildService.dropDownList();
	}
	
}
